package org.icress.fusion.moc

import org.icress.fusion.shiro.User
import org.apache.shiro.SecurityUtils
import org.springframework.transaction.annotation.*

class TcService {

    static transactional = false 
    
    def tbService
    
    @Transactional(readOnly = true)
    def list(Object params) {
        def tcInstanceList =Tc.list(params)
        return tcInstanceList
    }
    
    @Transactional(readOnly = true)
    def count(Object params) {
        def tcInstanceTotal = Tc.count()
        return tcInstanceTotal
    }


    def save(Tc tcInstance, Object params, boolean flush) {
        //get user
        def currentUser = User.findByUsername( SecurityUtils.getSubject().getPrincipal() )
        
        tcInstance.createdBy = currentUser
        tcInstance.lastModifiedBy = currentUser
        tcInstance.owner = currentUser
        
        if(!params["ti.id"] && params["ti001"]) {
            tcInstance.ti = Ti.findByTi001(params["ti001"])
        }
        
        def tbInstance = tbService.get(tcInstance.getTb().id)
        tcInstance.tc016 = tbInstance.getTb009()
        
        if(tcInstance.getTi()?.id){
            def tiInstance = Ti.get(tcInstance.getTi().id)
            tcInstance.tc017 = tiInstance.getTi002()
        }

        //tbInstance.tb007 = tbInstance.tb007 + tcInstance.tc002

        //2012-02-06
        //实际生产工时
        tcInstance.tc005 = (tcInstance.tc004.getTime() - tcInstance.tc003.getTime())/3600/1000 * tcInstance.tc001
        //单位标准工时
        tcInstance.standardWorkForce = (tcInstance.tc016 ?: tcInstance.tc017) ?: 0 //ERP标准工时-> 标准工时代号 -> 0
        //标准产出工时
        tcInstance.tc011 = tcInstance.standardWorkForce * (tcInstance.tc002 / tbInstance.tb012)       //班组完成量 div 极数
        //非生产总工时及报表总工时
        tcInstance.tc021 = 0
        tcInstance.tc018 = 0
        tcInstance.tgs.each{ 
            log.info("tg001=${it?.tg001}, tg002=${it?.tg002}")
            //报表总工时
            tcInstance.tc021 = tcInstance.tc021 + it?.tg001
            log.info "tc021=${tcInstance.tc021}"
            //非生产总工时
            tcInstance.tc018 = tcInstance.tc018 + it?.tg001 * it?.tg002
            log.info "tc018=${tcInstance.tc018}"
        }
        //报表总工时=非生产工时+实际生产工时
        tcInstance.tc021 = tcInstance.tc021 + tcInstance.tc005 
        //实际生产总工时=非生产工时K值分摊+实际生产工时
        tcInstance.tc010 = tcInstance.tc018 + tcInstance.tc005
        //作业效率=标准产出工时 / 实际生产总工时
        tcInstance.tc012 = tcInstance.tc011 / tcInstance.tc010
        //生产效率=标准产出工时 / ( 报表总工时 - 0.7 * 正常换线 )
        tcInstance.tc013 = tcInstance.tc011 / (tcInstance.tc021 - 0.7 * tcInstance.tc022) 

        def success = tcInstance.validate() && tcInstance.save(flush: flush)
               
        return success
    }
    
	def update(Tc tcInstance, Object params, boolean flush){
        def success = false
        Tc.withTransaction { status ->
            //delete tgs
            Tg.findAllByTc(tcInstance).each { it.delete(flush:true)}
            tcInstance.save(flush: flush)

            tcInstance.properties = params
            //get user
            def currentUser = User.findByUsername( SecurityUtils.getSubject().getPrincipal() )
            tcInstance.lastModifiedBy = currentUser

            def tbInstance = tbService.get(tcInstance.getTb().id)
            
            if(!params["ti.id"] && params["ti001"]) {
                tcInstance.ti = Ti.findByTi001(params["ti001"])
            }
            
            if(tcInstance.getTi()?.id){
                def tiInstance = Ti.get(tcInstance.getTi().id)
                tcInstance.tc017 = tiInstance.getTi002()
            }else{
                tcInstance.tc017 = null
            }
        //2012-02-06, 计算实际生产工时
        tcInstance.tc005 = (tcInstance.tc004.getTime() - tcInstance.tc003.getTime())/3600/1000 * tcInstance.tc001
        //单位标准工时
        tcInstance.standardWorkForce = (tcInstance.tc016 ?: tcInstance.tc017) ?: 0 //ERP标准工时-> 标准工时代号 -> 0
        //标准产出工时
        tcInstance.tc011 = tcInstance.standardWorkForce * (tcInstance.tc002 / tbInstance.tb012)       //班组完成量 div 极数
        //非生产总工时及报表总工时
        tcInstance.tc021 = 0
        tcInstance.tc018 = 0
        tcInstance.tgs.each{ 
            log.info("tg001=${it?.tg001}, tg002=${it?.tg002}")
            //报表总工时
            tcInstance.tc021 = tcInstance.tc021 + it?.tg001
            log.info "tc021=${tcInstance.tc021}"
            //非生产总工时
            tcInstance.tc018 = tcInstance.tc018 + it?.tg001 * it?.tg002
            log.info "tc018=${tcInstance.tc018}"
        }
        //报表总工时=非生产工时+实际生产工时
        tcInstance.tc021 = tcInstance.tc021 + tcInstance.tc005 
        //实际生产总工时=非生产工时K值分摊+实际生产工时
        tcInstance.tc010 = tcInstance.tc018 + tcInstance.tc005
        //作业效率=标准产出工时 / 实际生产总工时
        tcInstance.tc012 = tcInstance.tc011 / tcInstance.tc010
        //生产效率=标准产出工时 / ( 报表总工时 - 0.7 * 正常换线 )
        tcInstance.tc013 = tcInstance.tc011 / (tcInstance.tc021 - 0.7 * tcInstance.tc022) 

            success = tcInstance.validate() && tcInstance.save(flush: flush)
            if(success == false){
                status.setRollbackOnly()
            }
        }
        return success
	}
    
    def get(Object id) {
        def tcInstance = Tc.get(id)
        return tcInstance
    }
    
    def delete(Tc tcInstance, boolean flush) {
        def success = tcInstance.delete(flush: flush) 
        return success
    }

    def validateLockdate(date) {
        def ta = Ta.withCriteria(uniqueResult:true){
            order('lockDate', 'desc')
            maxResults(1)
        }
        
        return date >  ta.lockDate
    }
}
